package controller.student;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import sql.JDBC_T;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Vector;

public class StudentClassTableController {
    @FXML
    private Label stu_lb_classtable;
    @FXML
    private TableView stu_tblList_classtable;
//    public void initialize(){
//
//    }
    public  void update(String username) throws SQLException {
        if(username.isEmpty()){
            return;
        }
        JDBC_T jdbc_t = new JDBC_T();
        if(jdbc_t.is_conn()){
            System.out.println("课表更新");
        }
        String sql = MessageFormat.format("\n" +
                "SELECT course.Cname,course.Ctime,course.Csn\n" +
                "FROM\n" +
                "cscore,\n" +
                "(SELECT stuinfo.SID\n" +
                "FROM \n" +
                "stuinfo\n" +
                "where stuinfo.Sname = \"{0}\") as STU,course\n" +
                "WHERE STU.SID = cscore.SID AND cscore.CID = course.CID",username);
        PreparedStatement preparedStatement = jdbc_t.getConn().prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        Vector<String> courses = new Vector<String>();
        String single="";
         while(resultSet.next()){
                String course_name  = resultSet.getString("Cname");
                String date  = resultSet.getString("Ctime");
                String csn = resultSet.getString("Csn");
                single=" 课程名: "+course_name+" 星期" +date+"第"+csn+"节\n";
                courses.add(single);
        }
         String all ="";
        for (String i:courses
             ) {

            all+=i;
        }
        if(all.isEmpty()){
            stu_lb_classtable.textProperty().setValue("无课表");
        }
        else{
            stu_lb_classtable.textProperty().setValue(all);
        }

        System.out.println(sql);
    }

}
